package com.demo.forum.services.impl;

import com.demo.forum.common.Result;
import com.demo.forum.common.ResultCode;
import com.demo.forum.dao.BoardMapper;
import com.demo.forum.model.Board;
import com.demo.forum.model.exception.ApplicationException;
import com.demo.forum.services.IBoradService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
@Service
@Slf4j
public class BoardServiceImpl implements IBoradService {
    @Autowired
    private BoardMapper boardMapper;

    @Override
    public List<Board> selectByNum(Integer num) {
        if(num <= 0){
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            throw new ApplicationException(Result.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        List<Board> boards = boardMapper.selectByNum(num);
        return boards;

    }

    @Override
    public void addOneArticleCountById(Long id) {
        if(id == null){
            log.warn(ResultCode.ERROR_IS_NULL.toString());
            throw new ApplicationException(Result.failed(ResultCode.ERROR_IS_NULL));
        }

        Board board = boardMapper.selectByPrimaryKey(id);
        if(board == null){
            log.warn(ResultCode.FAILED_BOARD_NOT_EXISTS.toString());
            throw new ApplicationException(Result.failed
                    (ResultCode.FAILED_BOARD_NOT_EXISTS)
                     +", id =  "+id);
        }
        Board updateBoard = new Board();
        updateBoard.setId(board.getId());
        updateBoard.setArticleCount(board.getArticleCount()+1);
        int row = boardMapper.updateByPrimaryKeySelective(updateBoard);
        if(row != 1){
            log.warn(ResultCode.FAILED_BOARD_ARTICLE_COUNT.toString());
            throw  new ApplicationException(Result.failed(ResultCode.FAILED_BOARD_ARTICLE_COUNT)
            + ", id = " + id);
        }




    }

    @Override
    public void subOneArticleCountById(Long id) {
        if(id == null){
            log.warn(ResultCode.ERROR_IS_NULL.toString());
            throw new ApplicationException(Result.failed(ResultCode.ERROR_IS_NULL));
        }

        Board board = boardMapper.selectByPrimaryKey(id);
        if(board == null){
            log.warn(ResultCode.FAILED_BOARD_NOT_EXISTS.toString());
            throw new ApplicationException(Result.failed
                    (ResultCode.FAILED_BOARD_NOT_EXISTS)
                    +", board id =  "+id);
        }
        Board updateBoard = new Board();
        updateBoard.setId(board.getId());
        updateBoard.setArticleCount(board.getArticleCount()-1);
        if(updateBoard.getArticleCount() <0){
            updateBoard.setArticleCount(0);
        }
        int row = boardMapper.updateByPrimaryKeySelective(updateBoard);
        if(row != 1){
            log.warn(ResultCode.ERROR_SERVICES.toString());
            throw  new ApplicationException(Result.failed(ResultCode.ERROR_SERVICES));
        }

    }

    @Override
    public Board selectById(Long id) {
        if(id == null){
            log.warn(ResultCode.ERROR_IS_NULL.toString());
            throw new ApplicationException(Result.failed(ResultCode.ERROR_IS_NULL));
        }
        return boardMapper.selectByPrimaryKey(id);

    }
}
